﻿(function ($) {
    var emptytext = "", f2 = false, inline = false,
    AddTextBox = function (target) {
        if (!$(target).hasClass("focus")) {
            var content = $.trim($(target).text());
            var curfontsize = $(target).css('font-size');
            var h = $(target).parent().height() - 3;
            if (content.toLowerCase() == emptytext.toLowerCase()) {
                content = "";
            }
            var w = $(target).width() - 4;
            var faketxt = '<input type="text" style="width:96%;height:' + h + 'px; border:0; font-size:' + curfontsize + '; background-color:transparent;" value="' + $.trim(content) + '"/>';
            $(target).html(faketxt);
            $(target).attr("value", $.trim(content));

            var lastfocus = $(target).parent().children(".focus");
            OutCell(lastfocus);

            $(target).addClass("focus");
            $(target).children("input").focus();
            $(target).children("input").select();
        }

        var completesource = $(target).attr("completesource");
        if (typeof completesource != "undefined" && completesource != "") {
            $(target).children('input').autocomplete(window[completesource], { matchContains: true, width: 200, max: 200, cacheLength: 200 });
            $(target).children('input').focusout(function () {
                OutCell($(this).parent());
            });
        } else {
            $(target).children('input').focusout(function () {
                OutCell($(this).parent());
            });

            $(target).children('input').keydown(function (event) {
                var code = event.keyCode || event.which;
                switch (code) {
                    case 113:
                        f2 = true;
                        return false;
                    case 13:
                        if (inline)
                            break;
                        if (GoDown($(this).parent()))
                            OutCell($(this).parent());
                        f2 = false;
                        return false;
                    case 9:
                        if (event.shiftKey) {
                            if (GoBack($(this).parent()))
                                OutCell($(this).parent());
                        } else if (GoNext($(this).parent())) {
                            OutCell($(this).parent());
                        }
                        f2 = false;
                        return false;
                }
            });
        }
    },
    GoDown = function (cell) {
        var underrow = $(cell).parent().next("tr");
        if ($(underrow).length > 0) {
            var colindex = $(cell).parent().children("td").index(cell);
            var editable = $($(underrow).children()[colindex]).attr("editable");
            if (typeof editable != "undefined" && editable.toLowerCase() == "true") {
                AddTextBox($(underrow).children()[colindex]);
                f2 = false;
                return true;
            }
            else
                return false;
        }
    },
    GoNext = function (cell) {
        var next = $(cell).next('td[editable="true"]');
        if ($(next).length > 0) {
            AddTextBox(next);
            f2 = false;
            return true;
        } else
            return false;
    },
    GoBack = function (cell) {
        var next = $(cell).prev('td[editable="true"]');
        if ($(next).length > 0) {
            AddTextBox(next);
            f2 = false;
            return true;
        } else
            return false;
    },
    OutCell = function (cell) {
        $(cell).removeClass("focus");
        if ($(cell).children("input").length > 0) {
            var editcontnt = $.trim($(cell).children("input").val());
            var oldval = $(cell).attr("value");
            if (typeof oldval == "undefined" || oldval.toLowerCase() == emptytext.toLowerCase()) {
                oldval = "";
            }
            if (oldval.toLowerCase() != editcontnt.toLowerCase()) {
                $(cell).parent().attr("changed", true);
            }
            if (editcontnt == "") {
                editcontnt = emptytext;
            }
            $(cell).text(editcontnt);
            $(cell).textOverflow();
            $(cell).children("input").remove();
            f2 = false;
        }
    };
    $.extend($.fn, {
        InitialEditTableCell: function (options) {
            var o = $.extend({
                emptytext: "no check",
                inline: false
            }, options);
            emptytext = o.emptytext;
            inline = o.inline;
            f2 = false;
            $($(this).find('td[editable="true"]')).click(function () {
                if (!$(this).hasClass("focus")) {
                    AddTextBox(this);
                    f2 = true;
                }
            });
            $($(this).find('td[editable="true"]')).keydown(function (event) {
                if (!f2) {
                    var code = event.keyCode || event.which;
                    switch (code) {
                        //                        case 37:            
                        //                            var pre = $(this).prev('td[editable="true"]');            
                        //                            if ($(pre).length > 0) {            
                        //                                AddTextBox(pre);            
                        //                                OutCell(this);            
                        //                            }            
                        //                            break;            
                        case 38:
                            if (inline)
                                break;
                            var aboverow = $(this).parent().prev("tr");
                            if ($(aboverow).length > 0) {
                                var colindex = $(this).parent().children("td").index(this);
                                var editable = $($(aboverow).children()[colindex]).attr("editable");
                                if (typeof editable != "undefined" && editable.toLowerCase() == "true") {
                                    AddTextBox($(aboverow).children()[colindex]);
                                    OutCell(this);
                                }
                            }
                            break;
                        //                        case 39:            
                        //                            var next = $(this).next('td[editable="true"]');            
                        //                            if ($(next).length > 0) {            
                        //                                AddTextBox(next);            
                        //                                OutCell(this);            
                        //                            }            
                        //                            break;            
                        case 40:
                            if (inline)
                                break;
                            var underrow = $(this).parent().next("tr");
                            if ($(underrow).length > 0) {
                                var colindex = $(this).parent().children("td").index(this);

                                var editable = $($(underrow).children()[colindex]).attr("editable");
                                if (typeof editable != "undefined" && editable.toLowerCase() == "true") {
                                    AddTextBox($(underrow).children()[colindex]);
                                    OutCell(this);
                                }
                            }
                            break;
                        case 13:
                        case 113:
                            return false;
                    }
                    f2 = false;
                }
                return true;
            });
        }
    });
    $.extend($.fn, {
        InitialEditTableCellReport: function (options) {
            var o = $.extend({
                emptytext: "no check",
                inline: false
            }, options);
            emptytext = o.emptytext;
            inline = o.inline;
            f2 = false;
            $($(this).find('td[code="Report"]')).click(function () {
                if (!$(this).hasClass("focus")) {
                    AddTextBox(this);
                    f2 = true;
                }
            });
            $($(this).find('td[code="Report"]')).keydown(function (event) {
                if (!f2) {
                    var code = event.keyCode || event.which;
                    switch (code) {
                        case 38:
                            if (inline)
                                break;
                            var aboverow = $(this).parent().prev("tr");
                            if ($(aboverow).length > 0) {
                                var colindex = $(this).parent().children("td").index(this);
                                var editable = $($(aboverow).children()[colindex]).attr("editable");
                                if (typeof editable != "undefined" && editable.toLowerCase() == "true") {
                                    AddTextBox($(aboverow).children()[colindex]);
                                    OutCell(this);
                                }
                            }
                            break;
                        case 40:
                            if (inline)
                                break;
                            var underrow = $(this).parent().next("tr");
                            if ($(underrow).length > 0) {
                                var colindex = $(this).parent().children("td").index(this);

                                var editable = $($(underrow).children()[colindex]).attr("editable");
                                if (typeof editable != "undefined" && editable.toLowerCase() == "true") {
                                    AddTextBox($(underrow).children()[colindex]);
                                    OutCell(this);
                                }
                            }
                            break;
                        case 13:
                        case 113:
                            return false;
                    }
                    f2 = false;
                }
                return true;
            });
        }
    });
    $.extend($.fn, {
        DisableEditTableCell: function () {
            $($(this).find('td[editable="true"]')).unbind("click");
            $($(this).find('td[editable="true"]')).unbind("keydown");
        }
    });
    $.extend($.fn, {
        DoPendingChange: function () {
            $.each($(this).find("td.focus"), function (i, item) {
                OutCell(item);
            });
        }
    });
} (jQuery));